type CountdownOptions = {
  initialDuration: number
  onTimeout: () => void
}

export function startCountdown({ initialDuration, onTimeout }: CountdownOptions) {
  let duration = initialDuration
  const intervalId = setInterval(() => {
    duration--
    if (duration <= 0) {
      clearInterval(intervalId)
      onTimeout()
    }
  }, 1000)

  return {
    stop: () => {
      clearInterval(intervalId)
    },
    getRemainingTime: () => duration
  }
}
